package logic

import (
	"api/model/discount"
	"api/service"
	"api/utils"
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddBusDiscountLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddBusDiscountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddBusDiscountLogic {
	return &AddBusDiscountLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 【商家】添加折扣卡
func (l *AddBusDiscountLogic) AddBusDiscount(req *types.RequestAddDiscount) (resp *types.ResponseAddDiscount, err error) {

	conn := sqlx.NewMysql(l.svcCtx.Config.DataSource)
	businessService := service.NewBusinessService(l.ctx, l.svcCtx)
	business := businessService.GetLoginBusinessInfo()
	discountModel := discount.NewDiscountModel(conn)

	d := &discount.Discount{
		Discount:   req.Discount,
		Money:      req.Money,
		CreateAt:   utils.GetCurDateTime(),
		BusinessId: business.Id,
	}

	ret, err := discountModel.Insert(l.ctx, d)
	if err != nil {
		return &types.ResponseAddDiscount{
			Code: 200,
			Id:   0,
		}, nil
	}

	id, _ := ret.LastInsertId()
	return &types.ResponseAddDiscount{
		Code: 200,
		Id:   id,
	}, nil
}
